

#ifndef MY_GLOADCONFIGURATION
#define MY_GLOADCONFIGURATION


BOOL gLoadConfiguration(HINSTANCE hInstance) {

	BOOL bResult = TRUE;	
	size_t N = MAX_PATH * 2 * sizeof(WCHAR);
		
	PWSTR buf = (PWSTR)HeapAlloc(
	GetProcessHeap(), 
	HEAP_ZERO_MEMORY,
	N
	);
		
	DWORD res = GetModuleFileNameW(
		hInstance, 
		buf, 
		N
	);
		
	if (res == 0) {
		bResult = FALSE;			
	} else {
		wstring pathToDll = buf;
		g_params[g_idDLLPATH] = pathToDll;
		int m = pathToDll.length();			
		int pos = pathToDll.find_last_of(L".", m);
		if (pos > 0) {
			ZeroMemory(buf, N);
			pathToDll.copy(buf, pos, 0);				
			wstring pathToIni = buf;				
			pathToIni.append(L".ini");
			g_params[g_idINIPATH] = pathToIni;
				
			pos = 0;
			do {
				ZeroMemory(buf, N);
				const wchar_t* keyName = g_enKeys[pos];
				if (keyName == NULL) break;
				
				res = GetPrivateProfileStringW(
						g_SectionName,
						keyName,
						NULL,
						buf,
						N,
						pathToIni.c_str()
						);	
							
				if (res > 0) {
					wstring value = buf;
					g_params[keyName] = buf;
				} else {
					ZeroMemory(buf, N);
					wcscpy(buf, L"The application can't find the specified initialization file [");
					wcscat(buf, pathToIni.c_str());
					wcscat(buf, L"]  for a key: ");
					wcscat(buf, keyName);
					wcscat(buf, L" under the section heading specified [");
					wcscat(buf, g_SectionName);
					wcscat(buf, L"]!\r\n\r\nA section in the initialization file must have the following form: \r\n");
					wcscat(buf, L"\t[");
					wcscat(buf, g_SectionName);					
					wcscat(buf, L"]\r\n\t");
					wcscat(buf, keyName);
					wcscat(buf, L"=...\r\n");

					MessageBoxW(
						GetDesktopWindow(),
						buf,
						pathToIni.c_str(),
						MB_OK | MB_ICONERROR
					);
					bResult = FALSE;
				}
					
				pos++;
				
			} while(TRUE);
			
		} else {
			bResult = FALSE;
		}
	}
					
	HeapFree(GetProcessHeap(), 0, buf);		
	
	if (bResult) {
		wstring pathToDll = g_params[g_idDLLPATH];
		int m = pathToDll.length();			
		int pos = pathToDll.find_last_of(L"\\", m);
		wstring pathToDllDir = pathToDll.substr(0, pos + 1);
		wstring pathToTree = pathToDllDir;
		pathToTree.append(g_params[g_idTREEDIR]);
		g_params[g_idTREEDIR] = pathToTree;
	};
	
	return bResult;
}



#endif
